package com.haidong;

import com.haidong.mapper.UserMapper;
import com.haidong.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 这个类负责管理登录校验,用户管理等
 */
public class Manager {
    private static String resource = "mybatis-config.xml";
    private static InputStream inputStream;
    static {
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    private static SqlSession sqlSession = sqlSessionFactory.openSession();

    //密码校验
    public static int[] judgeUser(String id,String psw) throws Exception {
        UserMapper um = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setName(id);
        user.setPsw(psw);
        List<User> users = um.selectByCondition(user);
        if(users.size()==1 && users.get(0).getIsAdmin()==1){
            return new int[]{1,users.get(0).getId()};
        }else if(users.size()==1 && users.get(0).getIsAdmin()==0){
            return new int[]{2,users.get(0).getId()};
        }else{
            return new int[]{0,0};
        }
    }
    //新增一个用户到数据库
    public static void addUserToDataBase(String id,String psw,int isAdmin) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper um = sqlSession.getMapper(UserMapper.class);
        User add = new User();
        add.setName(id);
        add.setPsw(psw);
        add.setIsAdmin(isAdmin);
        um.insert(add);
        sqlSession.commit();
    }
    //在数据库中删除一个用户
    public static void deleteUser(String id,String psw) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper um = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setName(id);
        user.setPsw(psw);
        List<User> users = um.selectByCondition(user);
        for (User user1 : users) {
            um.delete(user1.getId());
        }
        sqlSession.commit();
    }
    public static void updateUser(int userId,String psw) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper um = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(userId);
        user.setPsw(psw);
        um.update(user);
        sqlSession.commit();
    }
}
